//
//  bottomView.swift
//  QQZone
//
//  Created by xmg on 16/9/1.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit


protocol BottomViewDelegate : class {

    func bottomMenu(bottomV:bottomView ,type : Int)
}


/** 设置底部按钮 */
class bottomView: UIView {
    /** 设置代理 */
    var delegate : BottomViewDelegate?
    /** 设置按钮 */
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpItem("tabbar_mood")
        setUpItem("tabbar_photo")
        setUpItem("tabbar_blog")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension bottomView{

    private func setUpItem(iconName : String){
    
        /** 创建按钮 */
        let item = UIButton()
        /** 设置按钮属性 */
        item.setImage(UIImage(named: iconName), forState: .Normal)
        item.setBackgroundImage(UIImage(named: "tabbar_separate_selected_bg"), forState: .Highlighted)
        item.addTarget(self, action: "itemClick:", forControlEvents: .TouchUpInside)
        item.tag = subviews.count
        addSubview(item)
    }
    
   @objc  private func itemClick (item : UIButton){
        delegate?.bottomMenu(self, type: item.tag);
    }
}
extension bottomView{

    func setUpScreenDirection(isBool :Bool){
          /** 获取子控件个数 */
        let count = subviews.count
        
        /** 设置frame */
        let W = superview?.frame.width
        let H = isBool ? kDockItemH : CGFloat(count) * kDockItemH
        let X : CGFloat = 0
        let Y = (superview?.frame.height)! - H
        
        frame = CGRect(x: X, y: Y, width: W!, height: H)
        
        /** 设置子控制的frame */
        for i in 0 ..< count{
             let item = subviews[i]
            
            let itemW = isBool ? (W! / CGFloat(count)) : W
            let itemH = kDockItemH
            let itemX : CGFloat = isBool ? CGFloat(i) * itemW! : 0
            let itemY : CGFloat = isBool ? 0 :CGFloat(i) * itemH
            
            item.frame = CGRect(x: itemX, y: itemY, width: itemW!, height: itemH)
            
        }
        
        
    }
}